package com.seeyon.apps.ssoAndDoc.utils;

import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.log.CtpLogFactory;
import com.seeyon.v3x.edoc.domain.EdocSummary;
import com.seeyon.v3x.edoc.manager.EdocSummaryManager;
import org.apache.commons.logging.Log;

import java.io.*;
import java.util.Map;

/**
* @ClassName: EDOC Download  
* @Description: 附件文件下载
* @author WXT_guyinchuan
* @date 2020-10-23  
*    
*/  
public class TheBodyOfTheDownload{

	private static final Log LOG = CtpLogFactory.getLog(TheBodyOfTheDownload.class);

	public static void downWenSingleFile(String oldPath,String  newPath)throws Exception {
		LOG.info("TWL-获取文单pdf开始");
		File oldFile=new File(oldPath);
		LOG.info("TWL-获取文单路径：" + newPath);
		File newFile = new File(newPath);
		// 确保父目录存在
		File parentDir = newFile.getParentFile();
		if (parentDir != null && !parentDir.exists()) {
			parentDir.mkdirs(); // 创建所有必要的父目录
		}
		// 判断是否存在newFile
		if (!newFile.exists()) {
			newFile.createNewFile();
		}
		InputStream in = new FileInputStream(oldFile);
		OutputStream out = new FileOutputStream(newFile);
		byte[] buffer = new byte[1024];
		int len;
		// 循环取出流中的数据
		while ((len = in.read(buffer)) != -1) {
			out.write(buffer, 0, len);
		}
		if(out != null){
			out.close();
		}
		if(in != null){
			in.close();
		}
		LOG.info("TWL-生成原正文结束");
	}
	
	
	
	/**  
	* @Title: down  
	* @Description: 下载公文正文格式为Excel
	* @author:guyinchuan
	* @oldFileUrl     源路径
	* @newFile  目标路径
	* @return void
	* @throws    
	*/ 
	public static void downxls(String oldFileUrl,String summaryId,String newxlsname) {
		
		try {
			File oldFile = new File(oldFileUrl);
			
			 File edocFromPdfFileFoder = new File(FairCopy.getEdocFromFileUrl(summaryId)+File.separator+summaryId);
				if(!edocFromPdfFileFoder.exists()) {
					edocFromPdfFileFoder.mkdirs();
				}
				EdocSummaryManager edocSummaryManager =(EdocSummaryManager)AppContext.getBean("edocSummaryManager");
				Long summaryid = Long.valueOf(summaryId);
				EdocSummary summary =	edocSummaryManager.findById(summaryid);
				String edocsubject = summary.getSubject();
				String regex = "[><:\"/\\|?*]";
				edocsubject = edocsubject.replaceAll(regex, "");
				edocsubject = edocsubject.replace(" ", "");
			String accessoryPath = edocFromPdfFileFoder+File.separator+edocsubject+"-正文.xls";
			File newFile = new File(accessoryPath);
			// 确保父目录存在
			File parentDir = newFile.getParentFile();
			if (parentDir != null && !parentDir.exists()) {
				parentDir.mkdirs(); // 创建所有必要的父目录
			}
			// 判断是否存在newFile
			if (!newFile.exists()) {
				newFile.createNewFile();
			}
			InputStream 	in = new FileInputStream(oldFile);
			OutputStream out = new FileOutputStream(newFile);
			byte[] buffer = new byte[1024];
			int len;
			// 循环取出流中的数据
				while ((len = in.read(buffer)) != -1) {
					out.write(buffer, 0, len);
	}
			} catch (IOException e) {
				// TODO Auto-generated catch block
			LOG.info("xls-----gu",e);
			}
		}
	/**  
	* @Title: down  
	* @Description: 下载公文正文格式为pdf
	* @author:guyinchuan
	* @oldFileUrl     源路径
	* @newFile  目标路径
	* @return void
	* @throws    
	*/ 
	public static void downpdf(String oldFileUrl,String summaryId) {
		try {
			File oldFile = new File(oldFileUrl);
			
			 File edocFromPdfFileFoder = new File(FairCopy.getEdocFromFileUrl(summaryId)+File.separator+summaryId);
				if(!edocFromPdfFileFoder.exists()) {
					edocFromPdfFileFoder.mkdirs();
				}
				EdocSummaryManager edocSummaryManager =(EdocSummaryManager)AppContext.getBean("edocSummaryManager");
				Long summaryid = Long.valueOf(summaryId);
				EdocSummary summary =	edocSummaryManager.findById(summaryid);
				String edocsubject = summary.getSubject();
				String regex = "[><:\"/\\|?*]";
				edocsubject = edocsubject.replaceAll(regex, "");
				edocsubject = edocsubject.replace(" ", "");
			String accessoryPath = edocFromPdfFileFoder+File.separator+edocsubject+"-正文.pdf";
			File newFile = new File(accessoryPath);
			// 确保父目录存在
			File parentDir = newFile.getParentFile();
			if (parentDir != null && !parentDir.exists()) {
				parentDir.mkdirs(); // 创建所有必要的父目录
			}
			// 判断是否存在newFile
			if (!newFile.exists()) {
				newFile.createNewFile();
			}
			InputStream 	in = new FileInputStream(oldFile);
			OutputStream out = new FileOutputStream(newFile);
			byte[] buffer = new byte[1024];
			int len;
			// 循环取出流中的数据
				while ((len = in.read(buffer)) != -1) {
					out.write(buffer, 0, len);
	}
			} catch (IOException e) {
				// TODO Auto-generated catch block
			LOG.info("xls-----gu",e);
			}
		}
	
	/**
	 * 
	 * <p>Title: createHtml
	 * <p>Description: TODO
	 * @author WeiLianTang
	 * @company www.microcental.net
	 * @date 2020年11月6日
	 * @param params
	 * @throws Exception 
	 */
	public static void createHtml(Map<String, Object> params) throws Exception{
		LOG.info("TWL-生成HTML文开始");
		String content = (String) params.get("content");
		EdocSummary summay = (EdocSummary) params.get("summay");
		String subject = (String) params.get("subject");
		String attachTempPath = (String) params.get("attachTempPath");
		String realPath = attachTempPath +File.separator+ subject + "-正文源文件.html";
		LOG.info("TWL-生成HTML文路径：" + realPath);
		File newFile = new File(realPath);
		// 确保父目录存在
		File parentDir = newFile.getParentFile();
		if (parentDir != null && !parentDir.exists()) {
			parentDir.mkdirs(); // 创建所有必要的父目录
		}
		if (!newFile.exists()) {
			newFile.createNewFile();
		}
		BufferedWriter bw = new BufferedWriter(new FileWriter(realPath));
		bw.write(content);//在创建好的文件中写入
		bw.close();//一定要关闭文件
		LOG.info("TWL-生成HTML文结束");
	}
	
	/**
	 * 
	 * <p>Title: downSourceFile
	 * <p>Description: TODO
	 * @author WeiLianTang
	 * @company www.microcental.net
	 * @date 2020年11月6日
	 * @param params
	 * @throws Exception 
	 */
	public static void downSourceFile(Map<String, Object> params) throws Exception {
		LOG.info("TWL-生成原正文开始");
		File oldFile = (File) params.get("oldFile");
		String subject = (String) params.get("subject");
		String attachTempPath = (String) params.get("attachTempPath");
		String suffix = (String) params.get("suffix");
		String realPath = attachTempPath + subject + "-正文源文件" + suffix;
		LOG.info("TWL-生成原正文路径：" + realPath);
		File newFile = new File(realPath);
		// 确保父目录存在
		File parentDir = newFile.getParentFile();
		if (parentDir != null && !parentDir.exists()) {
			parentDir.mkdirs(); // 创建所有必要的父目录
		}
		// 判断是否存在newFile
		if (!newFile.exists()) {
			newFile.createNewFile();
		}
		InputStream in = new FileInputStream(oldFile);
		OutputStream out = new FileOutputStream(newFile);
		byte[] buffer = new byte[1024];
		int len;
		// 循环取出流中的数据
		while ((len = in.read(buffer)) != -1) {
			out.write(buffer, 0, len);
		}
		if(out != null){
			out.close();
		}
		if(in != null){
			in.close();
		}
		LOG.info("TWL-生成原正文结束");
	}
	
	/**  
	* @Title: down  
	* @Description: 下载公文正文源文件
	* @author:guyinchuan
	* @oldFileUrl     源路径
	* @newFile  目标路径
	* @return void
	* @throws    
	*/ 
public static void downSourceFile(String oldSourceFile,String summaryId,String suffix) {
		
		try {
			File oldFile = new File(oldSourceFile);
			 File edocFromPdfFileFoder = new File(FairCopy.getEdocFromFileUrl(summaryId)+summaryId+File.separator);
				if(!edocFromPdfFileFoder.exists()) {
					edocFromPdfFileFoder.mkdirs();
				}
				EdocSummaryManager edocSummaryManager =(EdocSummaryManager)AppContext.getBean("edocSummaryManager");
				Long summaryid = Long.valueOf(summaryId);
				EdocSummary summary =	edocSummaryManager.findById(summaryid);
				String edocsubject = summary.getSubject();
				String regex = "[><:\"/\\|?*]";
				edocsubject = edocsubject.replaceAll(regex, "");
				edocsubject = edocsubject.replace(" ", "");
			String accessoryPath = edocFromPdfFileFoder+File.separator+edocsubject+"-正文源文件.doc";
			File newFile = new File(accessoryPath);
			// 确保父目录存在
			File parentDir = newFile.getParentFile();
			if (parentDir != null && !parentDir.exists()) {
				parentDir.mkdirs(); // 创建所有必要的父目录
			}
			// 判断是否存在newFile
			if (!newFile.exists()) {
				newFile.createNewFile();
			}
			InputStream 	in = new FileInputStream(oldFile);
			OutputStream out = new FileOutputStream(newFile);
			byte[] buffer = new byte[1024];
			int len;
			// 循环取出流中的数据
				while ((len = in.read(buffer)) != -1) {
					out.write(buffer, 0, len);
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
			LOG.info("xls-----gu",e);
			}
		}
	/**  
	* @Title: downAccessory  
	* @Description: 下载公文附件
	* @author:guyinchuan
	* @param @param summaryId   
	* @return void
	* @throws    
	*/ 
		public static void downAccessory(String oldSourceFile,String summaryId,String accessoryname) {
			try {
				File oldFile = new File(oldSourceFile);
				
				 File edocFromAccessoryFileFoder = new File(ZipUtils.EdocFromFileUrl(summaryId)+File.separator+summaryId+File.separator);
					if(!edocFromAccessoryFileFoder.exists()) {
						edocFromAccessoryFileFoder.mkdirs();
					}
					EdocSummaryManager edocSummaryManager =(EdocSummaryManager)AppContext.getBean("edocSummaryManager");
					Long summaryid = Long.valueOf(summaryId);
					EdocSummary summary =	edocSummaryManager.findById(summaryid);
					String edocsubject = summary.getSubject();
					String regex = "[><:\"/\\|?*]";
					edocsubject = edocsubject.replaceAll(regex, "");
					edocsubject = edocsubject.replace(" ", "");
				String accessoryPath = ZipUtils.EdocFromFileUrl(summaryId)+File.separator+summaryId+File.separator+edocsubject+"-附件-"+accessoryname;
				File newFile = new File(accessoryPath);
				// 确保父目录存在
				File parentDir = newFile.getParentFile();
				if (parentDir != null && !parentDir.exists()) {
					parentDir.mkdirs(); // 创建所有必要的父目录
				}
				// 判断是否存在newFile
				if (!newFile.exists()) {
					newFile.createNewFile();
				}
				InputStream 	in = new FileInputStream(oldFile);
				OutputStream out = new FileOutputStream(newFile);
				byte[] buffer = new byte[1024];
				int len;
				// 循环取出流中的数据
					while ((len = in.read(buffer)) != -1) {
						out.write(buffer, 0, len);
		}
				} catch (IOException e) {
					// TODO Auto-generated catch block
				LOG.info("xls-----gu",e);
				}
			}
		
	 
}



